﻿using IdGen;
using TinyPinyin.Core;
namespace Open.Core.Helpers
{
    public interface ICodeAndName
    {
        string Code { get; set; }
        string Name { get; set; }
        string PyCode { get; set; }
    }

    public interface ISort<T>
    {
        T Sort { get; set; }
    }

    public interface ITree<T>
    {
        T PId { get; set; }
    }

    public static class Extend
    {
        public static string GetItem2(this List<Tuple<int, string, object>> list, int item1)
        {
            var it = list.FirstOrDefault(l => l.Item1 == item1);
            return it?.Item2;
        }
        public static object GetItem3(this List<Tuple<int, string, object>> list, int item1)
        {
            var it = list.FirstOrDefault(l => l.Item1 == item1);
            return it?.Item3;
        }
        public static int ERPGetItem2(this List<Tuple<string, int, string>> list, string item1)
        {
            var it = list.FirstOrDefault(l => l.Item1 == item1);
            return it?.Item2 == null ? 0 : it.Item2;
        }
        public static string ERPGetItem3(this List<Tuple<string, int, string>> list, string item1)
        {
            var it = list.FirstOrDefault(l => l.Item1 == item1);
            return it?.Item3;
        }
        public static string GetPinYin(this string text)
        {
            if (string.IsNullOrEmpty(text)) return null;
            var strList = text.Select(c => PinyinHelper.GetPinyin(c).FirstOrDefault().ToString() ?? "");
            return string.Join("", strList);
        }
    }

    [AttributeUsage(AttributeTargets.Property)]
    public class NotAbpGenerator : Attribute
    {
    }
    [AttributeUsage(AttributeTargets.Property)]
    public class ExtendInfo : Attribute
    {
    }


    public class IdGenManager
    {

        //private static readonly int GeneratorId = AppConfigurations.GetFromEnv().GetValue<int>("IdGen.Generator");
        private static readonly int GeneratorId = 1;
        // Let's say we take april 1st 2020 as our epoch
        private static readonly DateTime epoch = new DateTime(2020, 4, 1, 0, 0, 0, DateTimeKind.Utc);

        // CreateAsync an ID with 45 bits for timestamp, 2 for generator-id 
        // and 16 for sequence
        private static readonly IdStructure structure = new IdStructure(45, 2, 16);

        // Prepare options
        private static readonly IdGeneratorOptions options = new IdGeneratorOptions(structure, new DefaultTimeSource(epoch));

        /// <summary>
        /// Id生成器
        /// </summary>
        public static readonly IdGenerator IdGen = new IdGenerator(GeneratorId, options);
    }

    /// <summary>
    /// 全局单据类型
    /// </summary>
    public enum GlobalBillType
    {
        销售单 = 1,
        退款单 = 2,
        积分兑换 = 3,
        快速收银 = 4,
        快速收银退款 = 5,
    }
}
